
let obj = {
    name: 'ws',
    title: 'good',
    timing: '2018-12-29',
    bio: 'www.baidu.com',
    _p: 'params'
}

let p = new Proxy(obj,{

    // 拦截读取属性
    get: function (target,key) {
        if (key === "name") {
            return target[key];
        }
        return target[key];
    },

    // 拦截设置属性
    set: function (target,key,value) {
        if (key === "title") {
            return target[key] = value;
        }
        return target[key];
    },
    // 拦截 key in obj 操作
    has: function (target,key) {
        if (key === "name") {
            return target[key];
        }
        return false;
    },
    // 拦截 delete
    deleteProperty (target,key) {
        if (key[0] === "_") {
            delete target[key];
            return true
        } else {
            return false;
        }
    }
});

let son = Object.create(p);

// proxy 是对一个对象的代理拦截，是一个中间传话的身份。
// new proxy () 接收两个参数，第一个是 向它传话的目标 target = {}
// 第二个就是传话的内容设置 handler = {get: function () {},set: function () {}}

/* console.log(p.name);
console.log(p.title);

console.log(`son - ` + son.name);
 */
/* console.log(p.name);
p.name = 'rr';
p.title = 'ttttt';
console.log(p.name);
console.log(p.title); */

//console.log('in - ',"title" in p);



/* console.log(delete p._p);
console.log(p); */

//console.log(Array.isArray(Object.keys(p)));

{
    // Reflect 拥有和 proxy 相同的方法

    let obj = {
        name :'web front end',
        title: 'vue',
        time: '2018-12-29',
        content: 'vue.js'
    };

    /* console.log(Reflect.get(obj,"name"));
    console.log(Reflect.set(obj,"name","designer"));
    console.log(obj); */

}


{
    // proxy Reflect的实际使用

    function validator (target,validator) { 
        // target -> 目标对象  validator -> 是配置项
        return new Proxy(target,{
            _validator: validator,
            set (target,key,value ,proxy) {
                if (target.hasOwnProperty(key)) {
                    let va = this._validator[key];
                }
            }
        })
    }




}